package com.gxa.saas.controller;

import com.gxa.saas.pojo.Company;
import com.gxa.saas.service.CompanyService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

/**
 * @author wanghai
 * @date 2022/6/6 18:53
 */
@Api(value = "公司信息界面功能的说明")
@RestController
@RequestMapping("/company")
@CrossOrigin
public class CompanyController {
    @Autowired
    CompanyService companyService;
    /**
     * 更新公司的基本信息
     * @param company
     * @return
     */
    @ApiOperation(value = "更新公司的基本信息的操作")
    @ApiImplicitParam(name = "company",value = "公司对象",paramType = "body",dataType = "Company")
    @PostMapping("/update")
    public Map setCompanyMessage(@RequestBody Company company){
        companyService.updateCompany(company);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");

        return jsonResult.getMap();
    }
    @ApiOperation(value = "上传公司logo的操作")
    @PostMapping("/uploadLogo")
    public Map uploadLogo(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        UploadController uploadController = new UploadController();
        String upload = uploadController.upload(multipartFile, request);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.set("filename",upload);

        return jsonResult.getMap();
    }
    /**
     * 上传多张图片
     * @param multipartFile
     * @param request
     * @return
     */
    @ApiOperation("上传公司宣传图册的操作")
    @PostMapping(value = "/uploadImages")
    public Map uploadMany(@RequestParam("files") MultipartFile[] multipartFile,HttpServletRequest request) throws IOException {
        System.out.println("数组的长度是："+multipartFile.length);
        UploadController uploadController = new UploadController();
        ArrayList<String> upload = new ArrayList<>();
        for (int i = 0; i < multipartFile.length; i++) {
            System.out.println("获取的图片是："+multipartFile[i].toString());
            upload.add(uploadController.upload(multipartFile[i],request));
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(upload);
        return jsonResult.getMap();
    }

    @ApiOperation("获取公司基本信息的操作")
    @PostMapping("/getCompany")
    public Map select(){
        Company company = companyService.selectCompany();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(company);
        return jsonResult.getMap();
    }
}
